package cn.com.dycc.meeting_assist.meeting_info.service;

import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import cn.com.dycc.meeting_assist.meeting_info.dao.MeetingInfoDao;
import cn.com.dycc.meeting_assist.meeting_info.entity.MeetingInfo;
import cn.com.dycc.meeting_assist.meeting_info.param.FindMeetingInfoParam;
import cn.com.dycc.meeting_assist.meeting_info.param.MeetingInfoParam;
import cn.com.dycc.meeting_assist.meeting_room.dao.RoomDao;
import cn.com.dycc.meeting_assist.meeting_room.entity.Room;
import cn.com.dycc.notice.dao.MessageDao;
import cn.com.dycc.notice.entity.MessageParam;
import cn.com.dycc.util.DateUtil;
import cn.com.dycc.util.Paging;

/**
 * 
 * Description: 会议信息管理业务层实现
 * Author: Liket
 * Version: 1.0
 * Create Date Time: 2018年10月26日 上午10:16:03.
 * Update Date Time: 
 * @see
 */
@Service("meetingInfoService")
public class MeetingInfoServiceImpl implements MeetingInfoService{
	
	@Resource
	private MeetingInfoDao meetingDao;
	
	@Resource
	private MessageDao messageDao;
	
	@Resource
	private RoomDao roomDao;
	
	@Resource(name="paging")
	private Paging paging;
	
		
	public int addMeetingInfo(HttpServletRequest req, MeetingInfo meeting)
			throws MeetingInfoNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String)session.getAttribute("org_id");
		String user_id = (String)session.getAttribute("id");
		if(org_id==null){
			throw new MeetingInfoNotFoundException("请重新登录");
		}
		if(meeting==null){
			throw new MeetingInfoNotFoundException("请填写会议信息");
		}
				
		if(meeting.getMeeting_name()==null||meeting.getMeeting_name().trim().isEmpty()){
			throw new MeetingInfoNotFoundException("请填写名称");
		}
		if(meeting.getMeeting_flag()==null||meeting.getMeeting_flag().trim().isEmpty()){
			throw new MeetingInfoNotFoundException("请填写会议类别标识");
		}
		if(meeting.getMeeting_date()==null||meeting.getMeeting_date().trim().isEmpty()){
			throw new MeetingInfoNotFoundException("请填写会议时间");
		}
		if(meeting.getRoom_id()==null||meeting.getRoom_id().trim().isEmpty()){
			throw new MeetingInfoNotFoundException("请选择会议室");
		}
		if(meeting.getDept_id()==null||meeting.getDept_id().trim().isEmpty()){
			throw new MeetingInfoNotFoundException("请选择会议主办部门");
		}
		
		meeting.setOrg_id(org_id);
		meeting.setCreate_date(DateUtil.getNowDate(DateUtil.TIME_FMT));
		meeting.setOpt_user_id(user_id);
		
		int n = meetingDao.addMeetingInfo(meeting);
		if(n!=1){
			throw new MeetingInfoNotFoundException("系统繁忙，添加失败");
		}
		
		if("2".equals(meeting.getMessage_type())){
			Room room = roomDao.findRoomById(meeting.getRoom_id(),org_id);
			String des = meeting.getMeeting_des();
			if(des==null){
				des="无";
			}
			MessageParam message = new MessageParam();
			message.setMessage_type(2);
			message.setContent(
					"会议："+meeting.getMeeting_name()
					+"<br/>"
					+"地点："+room.getRoom_name()
					+"<br/>"
					+"时间："+meeting.getMeeting_date()
					+"<br/>"
					+"会议描述："+des);
			message.setTitle("会议通知");
			message.setVis(0);
			
			String account = (String) session.getAttribute("account");
			String username = (String) session.getAttribute("username");
			String ip = (String) session.getAttribute("userip");
			
			message.setUser_id(user_id);
			message.setAccount(account);
			message.setUsername(username);
			message.setSend_ip(ip);
			
			Timestamp timeDate = DateUtil.getNowTimestamp();
			message.setSend_date(timeDate);		
			message.setOrg_id(org_id);		
			
			message.setAuto_send_date(timeDate);		
			message.setPush_message(1);
			message.setUpdate_date(timeDate);
			message.setUpdate_user_id(user_id);
			messageDao.addMessage1(message);
		}
		return n;
	}

	public int deleteMeetingInfo(HttpServletRequest req, String meeting_id) throws MeetingInfoNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String)session.getAttribute("org_id");
		if(org_id==null){
			throw new MeetingInfoNotFoundException("请重新登录");
		}
		if(meeting_id==null||meeting_id.trim().isEmpty()){
			throw new MeetingInfoNotFoundException("请选择删除的会议信息");
		}
		int n = meetingDao.deleteMeetingInfo(meeting_id,org_id);
		if(n!=1){
			throw new MeetingInfoNotFoundException("系统繁忙，删除失败");
		}
		return n;
	}

	public int updateMeetingInfo(HttpServletRequest req, MeetingInfo meeting)
			throws MeetingInfoNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String)session.getAttribute("org_id");
		String user_id = (String)session.getAttribute("id");
		if(org_id==null){
			throw new MeetingInfoNotFoundException("请重新登录");
		}
		if(meeting==null){
			throw new MeetingInfoNotFoundException("请填写会议信息");
		}
				
		if(meeting.getMeeting_name()==null||meeting.getMeeting_name().trim().isEmpty()){
			throw new MeetingInfoNotFoundException("请填写名称");
		}
		if(meeting.getMeeting_flag()==null||meeting.getMeeting_flag().trim().isEmpty()){
			throw new MeetingInfoNotFoundException("请填写会议类别标识");
		}
		if(meeting.getMeeting_date()==null||meeting.getMeeting_date().trim().isEmpty()){
			throw new MeetingInfoNotFoundException("请填写会议时间");
		}
		if(meeting.getRoom_id()==null||meeting.getRoom_id().trim().isEmpty()){
			throw new MeetingInfoNotFoundException("请选择会议室");
		}
		if(meeting.getDept_id()==null||meeting.getDept_id().trim().isEmpty()){
			throw new MeetingInfoNotFoundException("请选择会议主办部门");
		}
		
		
		
		MeetingInfoParam oldMeetingInfo = meetingDao.findMeetingInfoById(meeting.getMeeting_id(),org_id);
		if(oldMeetingInfo==null){
			throw new MeetingInfoNotFoundException("系统繁忙，修改失败");
		}
		
		MeetingInfo newMeetingInfo = new MeetingInfo();
		if(!meeting.getMeeting_name().equals(oldMeetingInfo.getMeeting_name())){
			newMeetingInfo.setMeeting_name(meeting.getMeeting_name());
		}
		if(!meeting.getMeeting_flag().equals(oldMeetingInfo.getMeeting_flag())){
			newMeetingInfo.setMeeting_flag(meeting.getMeeting_flag());
		}	
		if(!meeting.getMeeting_des().equals(oldMeetingInfo.getMeeting_des())){
			newMeetingInfo.setMeeting_des(meeting.getMeeting_des());
		}
		if(!meeting.getMeeting_date().equals(oldMeetingInfo.getMeeting_date())){
			newMeetingInfo.setMeeting_date(meeting.getMeeting_date());
		}
				
		if(!meeting.getRoom_id().equals(oldMeetingInfo.getRoom_id())){
			newMeetingInfo.setRoom_id(meeting.getRoom_id());
		}
		if(!meeting.getDept_id().equals(oldMeetingInfo.getDept_id())){
			newMeetingInfo.setDept_id(meeting.getDept_id());
		}
		if(!meeting.getUser_id().equals(oldMeetingInfo.getUser_id())){
			newMeetingInfo.setUser_id(meeting.getUser_id());
		}
		/*if(!meeting.getState().equals(oldMeetingInfo.getState())){
			newMeetingInfo.setState(meeting.getState());
		}*/
		
		newMeetingInfo.setOpt_user_id(user_id);		
		newMeetingInfo.setMeeting_id(meeting.getMeeting_id());
		newMeetingInfo.setOrg_id(org_id);
		int n = meetingDao.updateMeetingInfo(newMeetingInfo);
		if(n!=1){
			throw new MeetingInfoNotFoundException("系统繁忙，稍后再试");
		}
		return n;
	}

	public MeetingInfoParam findMeetingInfoById(HttpServletRequest req, String meeting_id)
			throws MeetingInfoNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String)session.getAttribute("org_id");
		if(org_id==null){
			throw new MeetingInfoNotFoundException("请重新登录");
		}
		if(meeting_id==null||meeting_id.trim().isEmpty()){
			throw new MeetingInfoNotFoundException("会议信息不存在");
		}
		MeetingInfoParam meeting = meetingDao.findMeetingInfoById(meeting_id,org_id);
		if(meeting==null){
			throw new MeetingInfoNotFoundException("系统繁忙，查询失败");
		}
		return meeting;
	}

	public List<MeetingInfoParam> findMeetingInfoByParam(HttpServletRequest req,FindMeetingInfoParam param)
			throws MeetingInfoNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String)session.getAttribute("org_id");
		if(org_id==null){
			throw new MeetingInfoNotFoundException("请重新登录");
		}
		Integer page = param.getPage();
		Integer limit = param.getLimit();
		Integer start = null;
		if(limit==null&&page!=null){
			limit = paging.getPageSize();
			start=(page-1)*limit;
		}
		if(page==null){
			start = null;
			limit = null;
		}
		if(limit!=null&&page!=null){
			start = limit*(page-1);
		}		
		param.setStart(start);	
		param.setOrg_id(org_id);
		List<MeetingInfoParam> list = meetingDao.findMeetingInfoByParam(param);
		return list;
	}

	public String countMeetingInfoByParam(HttpServletRequest req, FindMeetingInfoParam param)
			throws MeetingInfoNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String)session.getAttribute("org_id");
		if(org_id==null){
			throw new MeetingInfoNotFoundException("请重新登录");
		}
		param.setOrg_id(org_id);
		String count = meetingDao.countMeetingInfoByParam(param);
		return count;
	}

	
}
